#include "core/order/orderfactory.h"
#include "autoorderbase.h"

AutoOrderBase::AutoOrderBase()
: m_bars(0)
, m_enabled(false)
, m_needConfirm(false)
, m_autoOrderConfirm(0)
, m_returnContinue(false)
{
}

int AutoOrderBase::setIndex(int index)
{
    m_index = index;
    if (m_bars) {
        ;
    }
    return index;
}

Bars* AutoOrderBase::setBars(Bars *bars)
{
    m_bars = bars;
    return bars;
}

bool AutoOrderBase::OnNewTick(double price, time_t time)
{
    if (m_enabled) {
        m_returnContinue = true;
        if (!m_bars)
            return true;
        if (!m_bars->getCurrValid())
            return true;
        newTick(price, time);
        return m_returnContinue;
    }
    return true;
}

NormalOrder* AutoOrderBase::createBuyOrder(int time, const char *symbol, double price, double lots, double stoploss, double takeprofit, int magic)
{
    NormalOrder *order = OrderFactory::createBuyOrder(time, symbol, price, lots, stoploss, takeprofit);
    if (!order)
        return 0;
    if (getNeedConfirm()) {
        if (!getUserConfirm(order))
            return 0;
    }
    if (m_orderManager->insertOrder(order));
        return order;
    return 0;
}

NormalOrder* AutoOrderBase::createSellOrder(int time, const char *symbol, double price, double lots, double stoploss, double takeprofit, int magic)
{
    NormalOrder *order = OrderFactory::createSellOrder(time, symbol, price, lots, stoploss, takeprofit);
    if (!order)
        return 0;
    if (getNeedConfirm()) {
        if (!getUserConfirm(order))
            return 0;
    }
    if (m_orderManager->insertOrder(order));
        return order;
    return 0;
}

bool AutoOrderBase::getUserConfirm(OrderBase *order)
{
    if (!m_autoOrderConfirm)
        return true;
    m_returnContinue = false;
    if (m_autoOrderConfirm->getConfirm(order))
        return true;
    return false;
}